package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;

import com.sky.service.CategoryService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public Page<Category> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        return baseMapper.selectPage(
                new Page<>(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize()),
                new LambdaQueryWrapper<Category>()
                        .like(StringUtils.isNotBlank(categoryPageQueryDTO.getName()), Category::getName, categoryPageQueryDTO.getName())
                        .eq(categoryPageQueryDTO.getType() != null, Category::getType, categoryPageQueryDTO.getType())
                        .orderByDesc(Category::getCreateTime)
        );

    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {

        Category category = new Category();

        BeanUtils.copyProperties(categoryDTO, category);

        category.setStatus(StatusConstant.ENABLE);
        //创建时间
        category.setCreateTime(LocalDateTime.now());
        //更新时间
        category.setUpdateTime(LocalDateTime.now());
        //创建人
        category.setCreateUser(BaseContext.getCurrentId());
        //更新人
        category.setUpdateUser(BaseContext.getCurrentId());
        this.save(category);
    }

    @Override
    public void delete(Long id) {
        //先查询是否有菜品或者套餐关联了分类
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, id);
        Long dish = dishMapper.selectCount(queryWrapper);
        if (dish > 0) {
            throw new RuntimeException("当前分类下关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId, id);
        Long setmeal = setmealMapper.selectCount(queryWrapper1);
        if (setmeal > 0) {
            throw new RuntimeException("当前分类下关联了套餐，不能删除");
        }
        this.removeById(id);

    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        this.updateById(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Category::getId, id)
                .set(Category::getStatus, status);
        categoryMapper.update( null , updateWrapper);
    }

    @Override
    public List<Category> listByCategory(Integer type) {

        return baseMapper.selectList(new LambdaQueryWrapper<Category>()
                //如果type为空择不当作查询条件
                .eq(type != null, Category::getType, type)
                .eq(Category::getStatus,StatusConstant.ENABLE)
                .orderByAsc(Category::getType)
                .orderByDesc(Category::getCreateTime)
        );
    }

}